package com.jd.tools;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;

import com.jd.po.BatFile;

public class BatFileReader {

	public static String read(File src) {
		StringBuffer res = new StringBuffer();
		String line = null;
		try {
			BufferedReader reader = new BufferedReader(new FileReader(src));
			while ((line = reader.readLine()) != null) {
				res.append(line + "\r\n");
			}
			reader.close();
		} catch (FileNotFoundException e) { } catch (IOException e) {
			e.printStackTrace();
		}
		return res.toString();
	}

	public static boolean write(String cont, File dist) {
		try {
			BufferedWriter writer = new BufferedWriter(new FileWriter(dist));
			writer.write(cont);
			writer.flush();
			writer.close();
			return true;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}
	public static StringBuffer appendFiles(String fileStr) {
		System.out.println("选择的文件列表-------"+fileStr);
		String[] files = fileStr.split(",");
		System.out.println(files.length);
		StringBuffer sb = new StringBuffer();
		if(files.length>=0) {
			for(int i=0;i<files.length;i++) {
				String filePath = files[i].substring(0, files[i].lastIndexOf("/"));
				filePath= filePath.replace("/", "\\");
				filePath = filePath.replace("build", "SearchFiles");
				files[i]= files[i].replace("/", "\\");
				String s;
				if(files[i].endsWith("*")){
					s = "xcopy    "+files[i]+"    "+filePath+"\\ /d /e /y" + "\r\n";
				}else {
					s = "xcopy    "+files[i]+"    "+filePath+"\\ /d" + "\r\n";
				}
				sb.append(s);
			}
			return sb;
		}
		return null;
	}
	public static long forJava(File f1,File f2) throws Exception{
		long time=new Date().getTime();
		int length=2097152;
		FileInputStream in=new FileInputStream(f1);
		FileOutputStream out=new FileOutputStream(f2);
		byte[] buffer=new byte[length];
		while(true){
			int ins=in.read(buffer);
			if(ins==-1){
				in.close();
				out.flush();
				out.close();
				return new Date().getTime()-time;
			}else
				out.write(buffer,0,ins);
		}
	}
	public BatFileReader() {
	}
	public static Boolean createBatFile(BatFile bf,String group,String svnUrl) throws Exception {
		Properties prop = new FileTool().getProperties("Path.properties");
		String SearchFilesPath = prop.getProperty("SearchFilesPath");
		String SearchFilesPathTmp = prop.getProperty("SearchFilesPathTmp");
		String SvnUsername = prop.getProperty("SvnUsername");
		String SvnPassword = prop.getProperty("SvnPassword");
		String disk = prop.getProperty("disk");
		File srcTmp = new File(SearchFilesPathTmp);
		File src = new File(SearchFilesPath+"/"+bf.getProjectName()+"/"+DateTool.getCurrentYMD()+"/"+bf.getProjectName()+"_"+bf.getOnLineType()+"_"+bf.getTestType()+"_"+DateTool.getCurrentTime()+".bat");
		File path = new File(SearchFilesPath+"/"+bf.getProjectName()+"/"+DateTool.getCurrentYMD());
		path.mkdirs();
		System.out.println("开始复制");
		System.out.println("srcTmp---"+srcTmp);
		String cont = BatFileReader.read(srcTmp);
		if(cont.equals("")){
			System.out.println("找不到模板BAT文件!");
		}
		System.out.println(cont);
		System.out.println(forJava(srcTmp,src));
		System.out.println("复制结束");
		
		// 对得到的内容进行处理
		try{
			String filesStr = appendFiles(bf.getFilesStr()).toString();
			cont = cont.replace("xcopy", filesStr);
			cont = cont.replace("项目名称",bf.getProjectName());
			cont = cont.replace("上线类型",bf.getOnLineType());
			cont = cont.replace("测试类型",bf.getTestType());
			cont = cont.replace("时间",bf.getTime());
			cont = cont.replace("域名",bf.getDomainName());
			cont = cont.replace("组名",group);
			cont = cont.replace("SVN地址",svnUrl);
			cont = cont.replace("账号",SvnUsername);
			cont = cont.replace("密码",SvnPassword);
			cont = cont.replace("盘符",disk);

		}catch(Exception e) {
			e.printStackTrace();
			
		}
		System.out.println(cont);
		// 更新源文件
		return BatFileReader.write(cont, src);
	}
	public static void main(String[] args) throws Exception {
		File srcTmp = new File("c:/SearchFilesTmp.bat");
		File src = new File("C:/work/SearchFiles/1/2/3/");
		File src1 = new File("C:/work/SearchFiles/1/2/3/123.bat");
		src.mkdirs();
		forJava(srcTmp,src1);
	}

}
